<?php
//	$labelWidth = ($labelWidth ? ' style="width: ' . $labelWidth . '"' : $labelWidth);
    $labelWidth = '';
	$action = (isset($action) ? $action : true);
	if ($action === 'begin') {
		echo '<form' . Html::attributes($attributes) . '>';
		foreach($elements as $el) {
			if ($el instanceof Form_Field_Hidden) {
				echo $el->render() . chr(10);
			}
		}
		if  ($legend) {
			echo '<table><tr><td ' . $labelWidth . ' colspan="2">' . $legend . '</td></tr></table>' . chr(10);
		}
	}

	if ($action === 'fieldset' || $action === true) {
//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------
foreach($elements as $el) {
	if ($action === 'fieldset') {
		if ($el->fieldset !== $fieldset) {
			continue;
		}
	}
	if ($el->rendered) {
		continue;
	}
	$fieldsetElements = array();
	if ($el->fieldset) {
		echo '<fieldset style=""' . Html::attributes($fieldsets[$el->fieldset]['attributes']) . '>' . chr(10);
		if (!empty($fieldsets[$fieldset]['legend'])) {
			echo '<legend>' . Html::chars($fieldsets[$el->fieldset]['legend']) . '</legend>';
		}
		foreach ($fieldsets[$el->fieldset]['elements'] as $element) {
			$fieldsetElements[] = $elements[$element];
		}
	} else {
		foreach($elements as $falseElement) {
			if ($falseElement->fieldset === false) {
				$fieldsetElements[] = $falseElement;
			}
		}
	}
	echo '<div>';
	$cnt = 0;
	foreach ($fieldsetElements as $element) {
		switch (get_class($element)) {
			case 'Form_Field_Hidden':
				break;
			case 'Form_Field_Comment':
				echo $element->render();
				break;
			default:
                $required_error = $element->errors ? 'required_error' : '';



                if($element->errors){
                    $error_text = '';
                    $error_text .= '<ul class="form_errors">';
                    foreach ($element->errors as $error) {
                        $error_text.= '<li class="form_one_error">' . $error . '</li>';
                    }
                    $error_text.= '</ul>';
                }
                else $error_text = '';



//				echo '<div' . ($element->bunched ? ' class="bunched"' : ' class="form_issue"') . '>' . chr(10);
                echo '<div' . ($element->wrapper_class ? ' class="'.$element->wrapper_class.'"' : ' class="form_issue"') . '>' . chr(10);
				echo '<div' . $labelWidth . ($cnt == 0 ? ' class="form_left first"' : ' class="form_left"') . '>' . chr(10);
				echo '<label class="form_label" for="' . $element->composeId() . '"' . $labelWidth . '>'
				   . ($element->label ? $element->label : '&nbsp;')
//				   . ($element->required ? '&nbsp;<span class="form_required">*</span>' : '')
                    . ($element->required ? '&nbsp;<span class="form_required '.$required_error.'">*'.$error_text.'</span>' : '')
                    . (!$element->required && $element->errors ? '&nbsp;<span class="form_required unrequired_error">*'.$error_text.'</span> ' : '')
				   . '</label><br />' . chr(10);
				echo '</div>' . chr(10) . '<div' . ($cnt == 0 ? ' class="form_right first"' : ' class="form_right"') . '>';
				echo $element->render() . chr(10);
				if ((isset($element->warnings) ? $element->warnings : false)) {
					echo '<ul class="warnings">';
					foreach ($element->warnings as $warning) {
						echo '<li>' . $warning . '</li>';
					}
					echo '</ul>';
				}
                echo '</div>';

                echo '<div class="clear"></div>';
				echo '</div>' . chr(10);

//                if ($element->errors) {
//                    echo '<div class="errors">';
//                    foreach ($element->errors as $error) {
//                        echo '<div class="form_error">' . $error . '</div>';
//                    }
//                    echo '</div>';
//                }
				$cnt++;
				break;
//                echo '</div>' . chr(10);
		}
		$element->rendered = true;
	}
	echo '</div>';
	if ($action === 'fieldset') {
		echo '</fieldset>';
		break;
	}
}
//------------------------------------------------------
//------------------------------------------------------
//------------------------------------------------------
	}
	if ($action === 'end') {
		echo '</form>';
	}
?>